package com.leonzx.base.controller.job;

import com.leonzx.base.config.pager.Page;
import com.leonzx.base.config.pager.PageInfo;
import com.leonzx.base.controller.base.BaseController;
import com.leonzx.base.entity.system.JobAndTrigger;
import com.leonzx.base.service.system.JobService;
import org.quartz.ObjectAlreadyExistsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author:梁亮
 * @Description:
 * @Date: 2018/7/2 17:39
 */
@RestController
@RequestMapping("/ajax/system/job")
public class AjaxJobController extends BaseController {

    @Autowired
    private JobService jobService;

    @RequestMapping("/list")
    public Map<String, Object> list(@PageInfo(pageSize = Page.PAGE_SIZE, orderBy = "qrtz_job_details.JOB_GROUP", orderDiv = Page.Sort.ASC) Page<JobAndTrigger> page) {
        Map<String, Object> result = getMap();
        try {
            List<JobAndTrigger> jobAndTriggers = jobService.getJobAndTriggers();
            page.setResult(jobAndTriggers);
            result.put("count", page.getTotalCount());
            result.put("data", jobAndTriggers);
            result.put("code", "0");
            result.put("msg", "加载完成");
        } catch (Exception e) {
            logger.error("查询失败", e);
            addFailMessage(result, "查询失败");
        }
        return result;
    }

    @RequestMapping("/save")
    public Map<String, Object> save(JobAndTrigger jobAndTrigger) {
        Map<String, Object> result = getMap();
        try {
            jobService.jobAdd(jobAndTrigger.getJobName(), jobAndTrigger.getJobGroup(), jobAndTrigger.getCronExpression());
            addSuccessMessage(result, "添加成功");
        } catch (ObjectAlreadyExistsException e1) {
            logger.error("添加失败，已存在此任务");
            addFailMessage(result, "添加失败，已存在此任务");
        } catch (Exception e) {
            logger.error("添加失败", e);
            addFailMessage(result, "添加失败");
        }
        return result;
    }

    @RequestMapping("/pause")
    public Map<String, Object> pause(JobAndTrigger jobAndTrigger) {
        Map<String, Object> result = getMap();
        try {
            jobService.jobPause(jobAndTrigger.getJobName(), jobAndTrigger.getJobGroup());
            addSuccessMessage(result, "暂停成功");
        } catch (Exception e) {
            logger.error("暂停失败", e);
            addFailMessage(result, "暂停失败");
        }
        return result;
    }

    @RequestMapping("/resume")
    public Map<String, Object> resume(JobAndTrigger jobAndTrigger) {
        Map<String, Object> result = getMap();
        try {
            jobService.jobResume(jobAndTrigger.getJobName(), jobAndTrigger.getJobGroup());
            addSuccessMessage(result, "恢复成功");
        } catch (Exception e) {
            logger.error("恢复失败", e);
            addFailMessage(result, "恢复失败");
        }
        return result;
    }

    @RequestMapping("/delete")
    public Map<String, Object> delete(JobAndTrigger jobAndTrigger) {
        Map<String, Object> result = getMap();
        try {
            jobService.jobDelete(jobAndTrigger.getJobName(), jobAndTrigger.getJobGroup());
            addSuccessMessage(result, "删除成功");
        } catch (Exception e) {
            logger.error("删除失败", e);
            addFailMessage(result, "删除失败");
        }
        return result;
    }

    @RequestMapping("/update")
    public Map<String, Object> update(JobAndTrigger jobAndTrigger) {
        Map<String, Object> result = getMap();
        try {
            jobService.jobUpdate(jobAndTrigger.getJobName(), jobAndTrigger.getJobGroup(), jobAndTrigger.getCronExpression());
            addSuccessMessage(result, "更新成功");
        } catch (Exception e) {
            logger.error("更新失败", e);
            addFailMessage(result, "更新失败");
        }
        return result;
    }
}
